package com.oceansoft.core.jxkh.controller;

import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.DatetimeUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.common.util.StringUtil;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.domain.Khfk;
import com.oceansoft.core.jxkh.domain.Task;
import com.oceansoft.core.jxkh.service.ITaskService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by begcoffee on 2017/12/11.
 */
@Controller
@RequestMapping("/task")
public class TaskController {
    @Resource
    private ITaskService taskService;

    /*
    * 考核任务新增页面
    * */
    @RequestMapping(value="add")
    public String addPage(HttpServletRequest request, ModelMap model) {
        return "task/task_add";
    }

    @RequestMapping(value="edit/{dbid}")
    public String editPage(@PathVariable("dbid") Long dbid, HttpServletRequest request, ModelMap model) {
        try {
            Task task = taskService.getTaskById(dbid);
            model.addAttribute("task",task);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("dbid",dbid);
        return "task/task_edit";
    }

    /***
     * 新增任务提交
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="newTask.json")
    public void newTask(HttpServletRequest request, ModelMap model) {
        String year = request.getParameter("year");
        String basemark = request.getParameter("basemark");
        String type = request.getParameter("type");
        String startdate = request.getParameter("startdate");
        String enddate = request.getParameter("enddate");
        String check_no_ids = request.getParameter("check_no_ids");
        Message message = new Message();

        if (StringUtil.isNotNullAndBlank(check_no_ids) && check_no_ids.length() > 0) {
            String ss[] = check_no_ids.split(",");
                for (int i = 0; i < ss.length; i++) {
                    Task task = new Task();
                    task.setBasemark(Long.parseLong(basemark));
                    task.setType(type);
                    Date start = new Date(),end = new Date();
                    if("0".equals(type)){
                        task.setName(year+"年"+ss[i]+"月月度考核");
                        start = DatetimeUtil.formatString(year+"-"+ss[i]+"-"+startdate,"yyyy-MM-dd");
                        start = DatetimeUtil.addMonth(start,1);
                        end = DatetimeUtil.formatString(year+"-"+ss[i]+"-"+enddate,"yyyy-MM-dd");
                        end = DatetimeUtil.addMonth(end,1);
                    }else if("1".equals(type)){
                        task.setName(year+"年第"+ss[i]+"季度考核");
                        start = DatetimeUtil.formatString(year+"-"+(Integer.valueOf(ss[i])*3+1)+"-1","yyyy-MM-dd");
                        start = DatetimeUtil.addDay(start,Integer.valueOf(startdate)-1);
                        end = DatetimeUtil.formatString(year+"-"+(Integer.valueOf(ss[i])*3+1)+"-1","yyyy-MM-dd");
                        end = DatetimeUtil.addDay(end,Integer.valueOf(enddate)-1);
                    }
                    task.setCheckyear(year);
                    task.setCheckno(ss[i]);
                    task.setStartdate(start);
                    task.setEnddate(end);
                    try {
                        taskService.addTask(task);
                        message.setStatus(Constant.RESULT_SUCC);
                    } catch (Exception e) {
                        e.printStackTrace();
                        message.setStatus(Constant.RESULT_FAILD);
                        message.setBody(Constant.RESULT_FAILD_INFO);
                    }
                }
        }
        model.addAttribute("message",message);
    }

    /***
     * 保存任务
     * @param request
     * @param model
     */
    @RequestMapping(value="saveTask.json")
    public void saveTask(Task task,HttpServletRequest request, ModelMap model) {
        Message message = new Message();
        try {
            Task taskold = taskService.getTaskById(task.getDbid());
            task.setStatus(taskold.getStatus());
            taskService.saveTask(task);
            message.setStatus(Constant.RESULT_SUCC);
        } catch (Exception e) {
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(Constant.RESULT_FAILD_INFO);
        }
        model.addAttribute("message",message);
    }

    /***
     * 删除任务
     * @param dbid
     * @param model
     */
    @RequestMapping(value = "deleteTask/{dbid}.json")
    public void deleteTask(@PathVariable("dbid") String dbid, ModelMap model) {
        Message message = new Message();
        try {
            taskService.deleteTask(Long.parseLong(dbid));
            message.setStatus(Constant.RESULT_SUCC);
        } catch (Exception e) {
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(Constant.RESULT_FAILD_INFO);
        }
        model.addAttribute("message",message);
    }

    /***
     * 停用任务
     * @param dbid
     * @param model
     */
    @RequestMapping(value = "stopTask/{dbid}.json")
    public void stopTask(@PathVariable("dbid") String dbid,HttpServletRequest request, ModelMap model) {
        String type = request.getParameter("type");
        Message message = new Message();
        try {
            int result = taskService.stopTask(Long.parseLong(dbid),type);
            if(result > 0){
                message.setStatus(Constant.RESULT_SUCC);
            }else {
                message.setStatus(Constant.RESULT_FAILD);
                message.setBody(Constant.RESULT_FAILD_INFO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(Constant.RESULT_FAILD_INFO);
        }
        model.addAttribute("message",message);
    }

    /***
     * 考核任务列表查询
     * @param request
     * @param modelMap
     */
    @RequestMapping(value="taskData.json")
    public void statistics(HttpServletRequest request, ModelMap modelMap) {
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String checkyear = request.getParameter("check_year");
        String checktype = request.getParameter("check_type");
        String checkstatus = request.getParameter("check_status");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("offset",offset);
        paramap.put("limit",limit);
        paramap.put("order",order);
        paramap.put("checkyear",checkyear);
        paramap.put("checktype",checktype);
        paramap.put("checkstatus",checkstatus);

        Pagination  TaskPag= taskService.querybycondition(paramap,user);
        modelMap.addAttribute("rows",TaskPag.getList());
        modelMap.addAttribute("total",TaskPag.getTotalRows());
    }

    /*
    * 反馈设置页面
    * */
    @RequestMapping(value="fksz")
    public String fkszPage(HttpServletRequest request, ModelMap model) {
        try {
            Khfk khfk = taskService.getKhfkById(1L);
            model.addAttribute("khfk",khfk);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "task/khfksz";
    }

    /***
     * 保存反馈设置
     * @param request
     * @param model
     */
    @RequestMapping(value="saveKhfk.json")
    public void saveKhfk(Khfk khfk, HttpServletRequest request, ModelMap model) {
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        Message message = new Message();
        try {
            khfk.setUpdateuser(String.valueOf(user.getDbId()));
            taskService.saveKhfk(khfk);
            message.setStatus(Constant.RESULT_SUCC);
        } catch (Exception e) {
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(Constant.RESULT_FAILD_INFO);
        }
        model.addAttribute("message",message);
    }

}
